package space.jamestang.simpleci.server.handler

import io.ktor.server.application.Application
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.delete
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.put
import io.ktor.server.routing.routing
import org.ktorm.dsl.eq
import org.ktorm.entity.add
import org.ktorm.entity.removeIf
import org.ktorm.entity.sequenceOf
import org.ktorm.entity.toList
import org.ktorm.entity.update
import space.jamestang.simpleci.config.ConfigChecker
import space.jamestang.simpleci.database
import space.jamestang.simpleci.schema.BuildTool
import space.jamestang.simpleci.schema.BuildTools
import space.jamestang.simpleci.server.Data
import space.jamestang.simpleci.server.ERROR
import space.jamestang.simpleci.server.OK
import java.io.File
import kotlin.io.path.Path

fun Application.buildToolCRUD() {

    routing {

        get("/buildTools/available"){

            val baseDir = File(ConfigChecker.config.buildToolsPath!!)
            if (baseDir.exists() && baseDir.isDirectory){
                val files = baseDir.listFiles()

                val list = mutableListOf<BuildTool>()
                if (files!= null){
                    for (file in files){
                        if (file.isDirectory){
                            val path = Path(file.absolutePath)
                            val buildTool = BuildTool {
                                name = path.fileName.toString()
                                version = file.listFiles()?.map { it.name }?.firstOrNull() ?: ""
                                tag = ""
                                location = path.toString()
                                executable = ""
                            }
                            list.add(buildTool)
                        }
                    }
                }
                call.respond(Data(data = list))
            }else{
                call.respond(ERROR)
            }
        }

        get("/buildTools") {
            val data = database.sequenceOf(BuildTools).toList()
            call.respond(Data(data = data))
        }

        post("/buildTools") {
            val data = call.receive<BuildTool>()
            val result = database.sequenceOf(BuildTools).add(data)
            if (result == 1) {
                call.respond(OK)
            } else {
                call.respond(ERROR)
            }
        }

        put("/buildTools") {
            val data = call.receive<BuildTool>()
            val result = database.sequenceOf(BuildTools).update(data)
            if (result == 1) {
                call.respond(OK)
            } else {
                call.respond(ERROR)
            }
        }

        delete("/buildTools/{id}") {
            val id = call.parameters["id"] ?: return@delete
            val result = database.sequenceOf(BuildTools).removeIf { buildTools -> buildTools.id eq id.toInt() }
            if (result == 1) {
                call.respond(OK)
            } else {
                call.respond(ERROR)
            }
        }

    }
}